<?php

namespace App\Console\Commands;


use App\Services\Facades\Youzan;
use App\Services\Models\Orders;
use Illuminate\Console\Command;

use Carbon\Carbon;
    

/**
 * DayTotal
 * 
 */
class PullOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:pull';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $lists = Youzan::orderLists();
        logger('start order pull task total'.$lists['data']['total_results'], $lists);
        $lists = $lists['data']['full_order_info_list'];
        
        foreach ($lists as $k=>$v) {
            $orderInfo   = $v['full_order_info']['order_info'];
            $payInfo     = $v['full_order_info']['pay_info'];
            $addressInfo = $v['full_order_info']['address_info'];
            
            $orders      = $v['full_order_info']['orders'];
            $pay_time    =  $orderInfo['pay_time'];
            $tid         = $orderInfo['tid'];
            foreach ($orders as $order) {
                if (Orders::where('tid', $tid)->where('oid', $order['oid'])->first()) {
                    continue;
                }
                if (strstr($order['outer_item_id'], '/')) {
                    $item_id_arr = explode('/', $order['outer_item_id']);
                    $gnum = 1;
                    foreach ($item_id_arr as $outer_item_id) {
                        $goodsInfo = Youzan::queryGoods($outer_item_id)['data'];
                        logger('goodsInfo', $goodsInfo);
                        $payment = round($order['payment']/count($item_id_arr), 2);
                        Orders::create([
                            'tid' => $tid,
                            'oid'   => $order['oid'],
                            'gnum' => $gnum++,
                            'transaction' => $payInfo['transaction'][0],
                            'pay_time'    => $pay_time,
                            'goods_name'   => $goodsInfo['items'][0]['title'],
                            'receiver_name' => $addressInfo['receiver_name'],
                            'receiver_tel' => $addressInfo['receiver_tel'],
                            'num'   => $order['num'],
                            'price' => round($goodsInfo['items'][0]['price']/100/1.091 , 2),
                            'tax_total' => round($payment - $payment/1.091, 2),
                            'payment' =>  $payment,
                            'province' => $addressInfo['delivery_province'],
                            'city' => $addressInfo['delivery_city'],
                            'district' => $addressInfo['delivery_district'],
                            'address' => $addressInfo['delivery_address'],
                            'outer_item_id' => $outer_item_id,
                            'receiver_id' => $orderInfo['order_extra']['id_card_number'],
                            'is_package' => 1,
                        ]);
                    }
                } else {
                    Orders::create([
                        'tid' => $tid,
                        'gnum' => 1,
                        'transaction' => $payInfo['transaction'][0],
                        'pay_time'    => $pay_time,
                        'oid'   => $order['oid'],
                        'goods_name'   => $order['title'],
                        'receiver_name' => $addressInfo['receiver_name'],
                        'receiver_tel' => $addressInfo['receiver_tel'],
                        'num'   => $order['num'],
                        'price' => round($order['price']/1.091,2),
                        'tax_total' => round($order['price'] - $order['price']/1.091, 2),
                        'payment' => $order['payment'],
                        'province' => $addressInfo['delivery_province'],
                        'city' => $addressInfo['delivery_city'],
                        'district' => $addressInfo['delivery_district'],
                        'address' => $addressInfo['delivery_address'],
                        'outer_item_id' => $order['outer_item_id'],
                        'receiver_id' => $orderInfo['order_extra']['id_card_number'],
                        'is_package' => 0,
                    ]);
                }
            }
      
            logger("end order pull task");
        }
    }
}